package net.lab1024.sa.admin.module.business.supercomputer.datatask.service;

import java.time.LocalDateTime;
import java.util.List;

import net.lab1024.sa.admin.module.business.supercomputer.datasource.dao.DatasourceDao;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.bo.DataSourceBO;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.dao.DatataskDao;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.entity.DatataskEntity;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.form.DatataskAddForm;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.form.DatataskQueryForm;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.form.DatataskUpdateForm;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.vo.DatataskVO;
import net.lab1024.sa.admin.module.business.supercomputer.datatask.domain.vo.StatusVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 任务配置 Service
 *
 * @Author KeissCake
 * @Date 2024-09-26 21:17:52
 * @Copyright 暂无
 */

@Service
public class DatataskService {

    @Resource
    private DatataskDao datataskDao;

    @Resource
    private DatasourceDao datasourceDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<DatataskVO> queryPage(DatataskQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<DatataskVO> list = datataskDao.queryPage(page, queryForm);
        PageResult<DatataskVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(DatataskAddForm addForm) {
        DatataskEntity datataskEntity = SmartBeanUtil.copy(addForm, DatataskEntity.class);
        datataskDao.insert(datataskEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(DatataskUpdateForm updateForm) {
        DatataskEntity datataskEntity = SmartBeanUtil.copy(updateForm, DatataskEntity.class);
        datataskDao.updateById(datataskEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        datataskDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer taskId) {
        if (null == taskId){
            return ResponseDTO.ok();
        }

        datataskDao.deleteById(taskId);
        return ResponseDTO.ok();
    }
    public ResponseDTO<String> updateStatus(Integer taskId, Integer status,String updateUser) {
        datataskDao.updateTime(taskId, LocalDateTime.now());
        datataskDao.updateStatusById(taskId,status,updateUser);
        return ResponseDTO.ok();
    }

    //查询状态
    public StatusVO queryStatus(Integer taskId) {
        return datataskDao.queryStatus(taskId);
    }
    public ResponseDTO<String> addTask(Integer dataSourceId) {
        //1.根据主键获得DataSource表数据
        DataSourceBO dataSourceBO = new DataSourceBO();
        dataSourceBO = datasourceDao.getById(dataSourceId);
        System.out.println(dataSourceBO);
        //对获取到的数据进行处理
        DatataskEntity datataskEntity = new DatataskEntity();
        //设置外键
        datataskEntity.setDataSourceId(dataSourceBO.getDataSourceId());
        //设置公用字段信息
        datataskEntity.setCreateTime(LocalDateTime.now());
        datataskEntity.setUpdateTime(LocalDateTime.now());
        datataskEntity.setCreateUser(dataSourceBO.getCreateUser());
        datataskEntity.setUpdateUser(dataSourceBO.getCreateUser());

        //2.将数据进行封装传入Dao层进行数据添加,将信息进行添加
         datataskDao.addTask(datataskEntity);
        //执行成功
         return ResponseDTO.ok();
    }

    public String queryDataSourceType(Integer dataSourceId) {
        return datasourceDao.queryDataSourceType(dataSourceId);
    }
}
